﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace KD.Sms.UI_Sys
{
    public partial class frmUnattendedEdit : Form
    {
        public bool E_Add = true;

        private string cardId;

        private string E_VehicleLength = string.Empty;
        private string E_WidthLength = string.Empty;
        private string E_CarriageLength = string.Empty;
        private string E_HigthLength = string.Empty;

        private int CardNumberId;

        private string E_CardNameId = string.Empty;


        public frmUnattendedEdit()
        {
            InitializeComponent();
        }


        public frmUnattendedEdit(DataGridViewRow dataGridViewRow)
        {
            InitializeComponent();
            E_Add = false;
            cardId = dataGridViewRow.Cells["CarNum"].Value.ToString();

            this.textBox1.Text = dataGridViewRow.Cells["CarNum"].Value.ToString();
            this.textBox1.Text = cardId;

            this.txtTotalLength.Text = dataGridViewRow.Cells["TotalLength"].Value.ToString();//
            this.txtCLength.Text = dataGridViewRow.Cells["CLength"].Value.ToString();//
            this.txtTotalWidth.Text = dataGridViewRow.Cells["TotalWidth"].Value.ToString();
            this.txtCWidth.Text = dataGridViewRow.Cells["CWidth"].Value.ToString();
            this.txtCHeight.Text = dataGridViewRow.Cells["CHeight"].Value.ToString();
            this.txtPostion.Text = dataGridViewRow.Cells["Postion"].Value.ToString();

        }


        private void frmUnattendedEdit_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tools_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {

            if (e.Tool.Key == "Save")
            {
                decimal _dValue = 0;
                string _strMsg = "";
                if (!string.IsNullOrEmpty(this.txtPostion.Text))
                {
                    if (this.txtPostion.Text != "0")
                    {
                        string[] _arr = this.txtPostion.Text.Split(',');
                        foreach (string value in _arr)
                        {
                            decimal.TryParse(value, out _dValue);
                            if (_dValue <= 0)
                            {
                                _strMsg += string.Format("车辆拉筋输入参数不合规,[{0}]\r\n", value);

                            }
                        }
                    }
                }
                bool _bResult = false;
                if (E_Add)
                {

                    string _carNo, _tieBar;
                    decimal _dVehicleLength, _dHigthLength, _dWidthLength, _dCrriageLength;

                    //获取下拉框的值
                    decimal.TryParse(E_VehicleLength, out _dVehicleLength);
                    decimal.TryParse(E_WidthLength, out _dHigthLength);
                    decimal.TryParse(E_HigthLength, out _dWidthLength);
                    decimal.TryParse(E_CarriageLength, out _dCrriageLength);

                    //赋值
                    _carNo = textBox1.Text;
                    _dVehicleLength = Convert.ToDecimal(txtTotalLength.Text);
                    _dCrriageLength = Convert.ToDecimal(txtCLength.Text);
                    _dWidthLength = Convert.ToDecimal(txtCWidth.Text);
                    _dHigthLength = Convert.ToDecimal(txtCHeight.Text);
                    _tieBar = txtPostion.Text;

                    _bResult = DAL.SqlLite.Main.Instance.DoAddUnattended(textBox1.Text, txtTotalLength.Text.ToDecimalEx(), txtCLength.Text.ToDecimalEx(), txtCWidth.Text.ToDecimalEx(), txtCHeight.Text.ToDecimalEx(), txtPostion.Text,txtTotalWidth.Text.ToDecimalEx());
                }
                else
                {
                    _bResult = DAL.SqlLite.Main.Instance.DoUpdateUnattended(textBox1.Text, txtTotalLength.Text.ToDecimalEx(), txtCLength.Text.ToDecimalEx(), txtCWidth.Text.ToDecimalEx(), txtCHeight.Text.ToDecimalEx(), txtPostion.Text, txtTotalWidth.Text.ToDecimalEx());
                }
                if (_bResult)
                {
                    DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, string.Format("成功{0}无人值守[{1}]", E_Add ? "添加" : "修改", txtCLength.Text));

                    UI.MsgBox.Show(UI.MsgType.Information, "操作成功!");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                    UI.MsgBox.Show(UI.MsgType.Error, "操作失败!");
            }
            if (e.Tool.Key == "Cannel")
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
